from aip import AipSpeech
import pygame
import shutil
import os

""" 你的 APPID AK SK """
APP_ID = '9729136'
API_KEY = '8gBb5mb9aEu1Pf3sbuzw1qvU'
SECRET_KEY = 'H7gVkowAKSGiX6YmZ43665zLf7XBCh0o'
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

def init():
	pygame.mixer.init()
	if os.path.isdir("temp"):
		# 存在
		shutil.rmtree("temp")
	os.mkdir("temp")

def main():
	count = 0
	while True:
		count += 1
		str = input("输入文字:")
		result  = aipSpeech.synthesis(str, 'zh', 1, {
			'vol': 2,
		})
		# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
		if not isinstance(result, dict):
			with open("temp/{}.mp3".format(count), "wb") as f:
				f.write(result)
			pygame.mixer.music.load(r"temp/{}.mp3".format(count))
			pygame.mixer.music.play(1)



if __name__ == '__main__':
	init()
	main()